package com.example.springboot.controller;

import com.example.springboot.entity.Application;
import com.example.springboot.mapper.ApplicationMapper;
import com.example.springboot.mapper.ApprovalMapper;
import com.example.springboot.mapper.DeviceMapper;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/applications")
public class ApplicationController {
    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private ApprovalMapper approvalMapper;

    //用get请求查询数据库信息
    @GetMapping
    public List<Application> findAll(){
        return applicationMapper.findAll();//查询所有数据
    }

    //用post请求对数据库数据进行增加和修改
    @PostMapping
    public Integer save(@RequestBody Application application){
        application.setApplierId(userMapper.selectId(application.getApplierName()));
        application.setDeviceId(deviceMapper.selectByNumber(application.getDeviceNumber()));
        return applicationService.save(application);//往数据库插入或更新数据
    }

    //按照审批状态分页查询
    @GetMapping("/page")
    public Map<String,Object> findStatus(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize,
                                         @RequestParam String isApprove){
        pageNum = (pageNum - 1) * pageSize;
        Integer total = applicationMapper.selectTotal(isApprove);
        List<Application> data = applicationMapper.selectPage(pageNum,pageSize,isApprove);
        for(Application application : data){
            application.setDeviceNumber(deviceMapper.selectById(application.getDeviceId()));
            application.setApplierName(userMapper.selectName(application.getApplierId()));
        }
        Map<String,Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }

    //审批后用post请求对数据库数据进行增加和修改
    @PostMapping("/{id}")
//    public int updateApprove(@RequestBody Map<String,Object>map){
//        String mapId = map.get("id").toString(); //json格式转化成string
//        Integer id = Integer.parseInt(mapId);  //string转化成int
//        return applicationMapper.updateApprove(id);//往数据库插入或更新数据
//    }
    public int updateApprove(@PathVariable Integer id){

        Integer approvalId = approvalMapper.findLatest();
        applicationMapper.updateApproval(id,approvalId);
        return applicationMapper.updateApprove(id);//往数据库插入或更新数据
    }

    @GetMapping("/{id}")
    public List<Application> selectById(@PathVariable Integer id){
        List<Application> apps = applicationMapper.selectById(id);
        for(Application app : apps){
            app.setDeviceNumber(deviceMapper.selectById(app.getDeviceId()));
        }
        return apps;
    }

}
